package com.zzyl.controller.security;

import com.zzyl.base.ResponseResult;
import com.zzyl.dto.ResourceDto;
import com.zzyl.service.ResourceService;
import com.zzyl.vo.MenuVo;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.TreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.nio.file.Path;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/resource")
@Api(tags = "资源管理")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @ApiOperation("资源列表")
    @PostMapping("/list")
    public ResponseResult<List<ResourceVo>> resourceList(@RequestBody ResourceDto resourceDto) {
        List<ResourceVo> list = resourceService.findResourceList(resourceDto);
        return ResponseResult.success(list);
    }

    @ApiOperation("资源树形")
    @PostMapping("/tree")
    public ResponseResult<TreeVo> resourceTreeVo(@RequestBody ResourceDto resourceDto) {
        TreeVo treeVo = resourceService.resourceTreeVo(resourceDto);
        return ResponseResult.success(treeVo);
    }

    @PutMapping
    @ApiOperation("资源添加")
    public ResponseResult createResource(@RequestBody ResourceDto resourceDto) {
        resourceService.createResource(resourceDto);
        return ResponseResult.success();
    }

    /**
     * 资源修改
     * @param dto
     * @return
     */
    @ApiOperation("资源修改")
    @PatchMapping()
    public ResponseResult updateResource(@RequestBody ResourceDto dto){
        resourceService.updateResource(dto);
        return ResponseResult.success();
    }

    @ApiOperation("启用禁用")
    @PostMapping("/enable")
    public ResponseResult enableOrDisable(@RequestBody ResourceDto dto){
        resourceService.enableOrDisable(dto);
        return ResponseResult.success();
    }

    /**
     * 删除菜单
     * @param menuId
     * @return
     */
    @ApiOperation("删除菜单")
    @DeleteMapping("/{menuId}")
    public ResponseResult delete(@PathVariable String menuId){
        resourceService.delete(menuId);
        return ResponseResult.success();
    }

    ////////////////////////////////////////////////////////////////////////////////////

    @GetMapping("/menus")
    @ApiOperation("我的菜单")
    public ResponseResult<List<MenuVo>> getMyMenus() {
        log.info("我的菜单");
        List<MenuVo> menus = resourceService.getMyMenus();
        return ResponseResult.success(menus);
    }
}
